1835B - Lottery - CodeForces Solution


binary search brute force greedy math sortings two pointers

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define pb push_back
#define ll long long int
#define fi first
#define se second
 
#define md1 1000000007
#define md2 998244353 //r=3  upto root of 1<<23
#define md3 1000000009
#define md4 7340033   //r=5  upto root of 1<<20
 
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
 
using namespace __gnu_pbds;
using namespace std;
 
// ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
 
ll n,m,k,a[1000006];
int main(){
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cin>>n>>m>>k;
    for(int i=1;i<=n;i++)cin>>a[i];
    a[0]=-1;
    a[n+1]=m+1;
    sort(a+1,a+1+n);
    ll maxCnt=0,ans=0;
    a[n+1]=m+1;
    a[0]=-1;
    for(int i=1;i<=n;){
        int j=i;
        for(;j<=n && a[i]==a[j];j++);
        j--;
        ll L=0,R=0;
        if(j>k-1)L=(a[j]+a[j-(k-1)])/2+1;
        else L=0;
        if(i<n-(k-1)+1)R=(a[i]+a[i+k-1]+1)/2-1;
        else R=m;

        if(R-L+1>maxCnt){
            maxCnt=R-L+1;
            ans=a[i];
        }
        else if(R-L+1==maxCnt){
            maxCnt=R-L+1;
            ans=min(ans,a[i]);
        }

        i=j+1;
    }
    for(int i=0;i<=n+1;i++){
        ll L=0,R=0;
        if(i<n+1 && a[i]+1<a[i+1]){
            if(i>k-1)L=(a[i]+1+a[i-(k-1)])/2+1;
            else L=0;
            if(i<n-k+1)R=(a[i]+1+a[i+k]+1)/2-1;
            else R=m;
            if(R-L+1>maxCnt){
                maxCnt=R-L+1;
                ans=a[i]+1;
            }
            else if(R-L+1==maxCnt){
                maxCnt=R-L+1;
                ans=min(ans,a[i]+1);
            }
            if(a[i]+2<a[i+1]){
                if(i>k-1)L=(a[i]+2+a[i-(k-1)])/2+1;
                else L=0;
                if(i<n-k+1)R=(a[i]+2+a[i+k]+1)/2-1;
                else R=m;
                if(R-L+1>maxCnt){
                    maxCnt=R-L+1;
                    ans=a[i]+2;
                }
                else if(R-L+1==maxCnt){
                    maxCnt=R-L+1;
                    ans=min(ans,a[i]+2);
                }   
            }
        }
        if(i>0 && a[i-1]<a[i]-1){
            if(i>k)L=(a[i]-1+a[i-k])/2+1;
            else L=0;
            if(i<n-(k-1)+1)R=(a[i]-1+a[i+(k-1)]+1)/2-1;
            else R=m;
 
            // cout<<L<<" "<<R<<" "<<a[i]-1<<"\n";
            if(R-L+1>maxCnt){
                maxCnt=R-L+1;
                ans=a[i]-1;
            }
            else if(R-L+1==maxCnt){
                maxCnt=R-L+1;
                ans=min(ans,a[i]-1);
            }
            if(a[i-1]<a[i]-2){
                if(i>k)L=(a[i]-2+a[i-k])/2+1;
                else L=0;
                if(i<n-(k-1)+1)R=(a[i]-2+a[i+(k-1)]+1)/2-1;
                else R=m;
    
                // cout<<L<<" "<<R<<" "<<a[i]-1<<"\n";
                if(R-L+1>maxCnt){
                    maxCnt=R-L+1;
                    ans=a[i]-2;
                }
                else if(R-L+1==maxCnt){
                    maxCnt=R-L+1;
                    ans=min(ans,a[i]-2);
                }
            }
        }
        // cout<<L[i]<<" "<<R[i]<<"\n";
    }
    cout<<maxCnt<<" "<<ans;
    return 0;
}
//https://codeforces.com/contest/1835/problem/B


Comments

Submit
0 Comments
More Questions

361A - Levko and Table
5E - Bindian Signalizing
687A - NP-Hard Problem
1542C - Strange Function
961E - Tufurama
129D - String
888A - Local Extrema
722B - Verse Pattern
278A - Circle Line
940A - Points on the line
1742C - Stripes
1742F - Smaller
1742B - Increasing
1742A - Sum
1742D - Coprime
390A - Inna and Alarm Clock
1398B - Substring Removal Game
1742G - Orray
416B - Art Union
962A - Equator
803B - Distances to Zero
291A - Spyke Talks
1742E - Scuza
1506D - Epic Transformation
1354G - Find a Gift
1426F - Number of Subsequences
1146B - Hate "A"
1718C - Tonya and Burenka-179
834A - The Useless Toy
1407D - Discrete Centrifugal Jumps